﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json.Linq;
using ZBZH_Manage_Order.Models;
using ZBZH_Manage_Order.PublicMethod;

namespace ZBZH_Manage_Order.Controllers.SystemSettings
{
    public class SystemLogController : BaseController
    {
        //
        // GET: /SystemLog/

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取所有系统日志
        /// </summary>
        /// <param name="jqgridparam"></param>
        /// <returns></returns>
        public ActionResult GetSystemLog(string Time_Start, string Time_End, string LogType, string Operat_Result, JqGridParam jqgridparam)
        {
            try
            {
                if (Operat_Result != null && Operat_Result != "")
                {
                    switch (Operat_Result)
                    {
                        case "-8989":
                            Operat_Result = "";
                            break;
                        case "1":
                            Operat_Result = "成功";
                            break;
                        case "2":
                            Operat_Result = "失败";
                            break;
                        case "3":
                            Operat_Result = "异常";
                            break;
                    }
                }
                if (Time_Start != null && Time_Start != "")
                {
                    Time_Start = Time_Start.Replace("-", "");
                }
                if (Time_End != null && Time_End != "")
                {
                    Time_End = Time_End.Replace("-", "");
                }

                List<Model.View_Order_OperationFlowInfo> list = new List<Model.View_Order_OperationFlowInfo>();
                string result = SRS.GetOrder_OperationFlow(jqgridparam.page, jqgridparam.rows, Time_Start, Time_End, LogType, Operat_Result, YZM, 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    if (jieguo.Count > 0)
                    {
                        list = ParseClass<Model.View_Order_OperationFlowInfo>.JsonList(jieguo);//解析返回的结果\
                        foreach (Model.View_Order_OperationFlowInfo item in list)
                        {
                            item.OperationTime = DateTimeHelper.ToDate2(item.OperationTime);
                            if (item.OperationTime.Length == 14)
                            {
                                item.OperationTime = DateTimeHelper.ToDate2(item.OperationTime);
                            }
                        }
                    }
                    //return list;
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = list
                    };
                    return Json(data);
                }
                else
                {
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = list
                    };
                    return Json(data);
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// 获取所有系统日志列表
        /// </summary>
        /// <returns></returns>
        public List<Model.View_Order_OperationFlowInfo> GetAllSystemLog(string Time_Start, string Time_End, string LogType, string Operat_Result, JqGridParam jqgridparam)
        {

            if (LogType != null && LogType != "")
            {
                switch (LogType)
                {
                    case "1":
                        LogType = "成功";
                        break;
                    case "2":
                        LogType = "失败";
                        break;
                    case "3":
                        LogType = "异常";
                        break;
                }
            }
            if (Time_Start != null && Time_Start != "")
            {
                Time_Start = Time_Start.Replace("-", "");
            }
            if (Time_End != null && Time_End != "")
            {
                Time_End = Time_End.Replace("-", "");
            }

            List<Model.View_Order_OperationFlowInfo> list = new List<Model.View_Order_OperationFlowInfo>();
            string result = SRS.GetOrder_OperationFlow(jqgridparam.page, jqgridparam.rows, Time_Start, Time_End, LogType, Operat_Result, YZM, 1);
            JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
            string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
            if (bs == "1")//返回结果正常
            {
                IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                if (jieguo.Count > 0)
                {
                    list = ParseClass<Model.View_Order_OperationFlowInfo>.JsonList(jieguo);//解析返回的结果\
                    //foreach (JToken item in jieguo)
                    //{
                    //    List<Model.View_Order_OperationFlowInfo> newlist = new List<Model.View_Order_OperationFlowInfo>();
                    //    Model.View_Order_OperationFlowInfo m = new Model.View_Order_OperationFlowInfo();
                    //    m.OperationFlowID = int.Parse(item["OperationFlowID"].ToString());
                    //    m.OperationTitle = item["OperationTitle"].ToString();
                    //    m.OperationText = item["OperationText"].ToString();
                    //    m.OperationType = item["OperationType"].ToString();
                    //    m.OperationRes = item["OperationRes"].ToString();
                    //    m.OperationUser = int.Parse(item["OperationUser"].ToString());
                    //    m.OperationTime = item["OperationTime"].ToString();
                    //    m.OperationBZ = item["OperationBZ"].ToString();
                    //    m.OperationYL = item["OperationYL"].ToString();
                    //    m.DictName = item["DictName"].ToString();
                    //    m.OperNum = item["OperNum"].ToString();
                    //    list.Add(m);
                    //}
                }
                return list;
                //var data = new
                //{
                //    total = searcho["total"].ToString(),
                //    rows = list
                //};
                //return Json(data);
            }
            else
            {
                return null;
            }
        }

        public ActionResult GetLogType()
        {
            List<Basic_Dict_T> ls = new List<Basic_Dict_T>();
            ls = PublicMethod.GetDict.GetList(35);

            // 添加全部选项按钮
            Basic_Dict_T add = new Basic_Dict_T();
            add.DictName = "全部";
            add.DictKey = "";
            ls.Insert(0, add);
            return Json(ls, JsonRequestBehavior.AllowGet);
        }
    }
}
